﻿using System;
using System.Text;
using System.Web.UI.WebControls;

namespace DigiCardDeck
{
    public partial class Editor : System.Web.UI.Page
    {
        private string stConnectionString = string.Empty;
        private const string DECK_DB = "deckdb";
        private string deckNo = string.Empty;
        private string id = string.Empty;
        private string password = string.Empty;

        protected void Page_Load(object sender, EventArgs e)
        {
            deckNo = Request.QueryString["deckNo"];
            id = Request.QueryString["value"];

            // 接続文字列を生成する
            stConnectionString += "Data Source         = " + CodeConst.DATA_SOURCE + ";";
            stConnectionString += "Initial Catalog     = " + CodeConst.INITIAL_CATALOG + ";";
            //stConnectionString += "Integrated Security = SSPI;";
            stConnectionString += "User ID             = " + CodeConst.USER_ID + ";";
            stConnectionString += "Password            = " + CodeConst.PASSWORD + ";";

            Label1.Text = "必要な情報を入力し、登録ボタンを押してください。カードリストの欄には、カード検索から画像のURLを指定してください。";
            TextBox2.TextMode = TextBoxMode.Password;

            Label1.Text += deckNo;

            if ( !IsPostBack && deckNo!= null && id != null)
            {
                Button1.Text = "更新";

                // SqlConnection の新しいインスタンスを生成する (接続文字列を指定)
                System.Data.SqlClient.SqlConnection cSqlConnection = (
                    new System.Data.SqlClient.SqlConnection(stConnectionString)
                );

                try
                {
                    // データベース接続を開く
                    cSqlConnection.Open();

                    // cSqlConnection から SqlCommand のインスタンスを生成する
                    System.Data.SqlClient.SqlCommand hCommand = cSqlConnection.CreateCommand();

                    hCommand.CommandText = "select * from deckdb where ID = '" + id + "' and  deckno = " + deckNo;

                    // 指定した SQL コマンドを実行する
                    System.Data.SqlClient.SqlDataReader cReader = hCommand.ExecuteReader();

                    while (cReader.Read())
                    {
                        password = cReader["Password"].ToString();

                        TextBox1.Text = cReader["Id"].ToString();
                        TextBox3.Text = cReader["DeckName"].ToString();
                        TextBox4.Text = cReader["Remarks"].ToString();

                        TextBox5.Text = GetUrlValue(cReader["URL1"].ToString());
                        TextBox6.Text = GetUrlValue(cReader["URL2"].ToString());
                        TextBox7.Text = GetUrlValue(cReader["URL3"].ToString());
                        TextBox8.Text = GetUrlValue(cReader["URL4"].ToString());
                        TextBox9.Text = GetUrlValue(cReader["URL5"].ToString());
                        TextBox10.Text = GetUrlValue(cReader["URL6"].ToString());
                        TextBox11.Text = GetUrlValue(cReader["URL7"].ToString());
                        TextBox12.Text = GetUrlValue(cReader["URL8"].ToString());
                        TextBox13.Text = GetUrlValue(cReader["URL9"].ToString());
                        TextBox14.Text = GetUrlValue(cReader["URL10"].ToString());
                        TextBox15.Text = GetUrlValue(cReader["URL11"].ToString());
                        TextBox16.Text = GetUrlValue(cReader["URL12"].ToString());
                        TextBox17.Text = GetUrlValue(cReader["URL13"].ToString());
                        TextBox18.Text = GetUrlValue(cReader["URL14"].ToString());
                        TextBox19.Text = GetUrlValue(cReader["URL15"].ToString());
                        TextBox20.Text = GetUrlValue(cReader["URl16"].ToString());
                        TextBox21.Text = GetUrlValue(cReader["URL17"].ToString());
                        TextBox22.Text = GetUrlValue(cReader["URL18"].ToString());
                        TextBox23.Text = GetUrlValue(cReader["URL19"].ToString());
                        TextBox24.Text = GetUrlValue(cReader["URL20"].ToString());
                        TextBox25.Text = GetUrlValue(cReader["URL21"].ToString());
                        TextBox26.Text = GetUrlValue(cReader["URL22"].ToString());
                        TextBox27.Text = GetUrlValue(cReader["URL23"].ToString());
                        TextBox28.Text = GetUrlValue(cReader["URL24"].ToString());
                        TextBox29.Text = GetUrlValue(cReader["URL25"].ToString());
                        TextBox30.Text = GetUrlValue(cReader["URL26"].ToString());
                        TextBox31.Text = GetUrlValue(cReader["URL27"].ToString());
                        TextBox32.Text = GetUrlValue(cReader["URL28"].ToString());
                        TextBox33.Text = GetUrlValue(cReader["URL29"].ToString());
                        TextBox34.Text = GetUrlValue(cReader["URL30"].ToString());
                    }

                    cReader.Dispose();

                    hCommand.Dispose();

                }
                catch (Exception ex)
                {

                }
                finally
                {
                    // データベース接続を閉じる
                    cSqlConnection.Close();
                    cSqlConnection.Dispose();

                }
            }
        }

        protected void Button1_Click(object sender, EventArgs e)
        {
            Label1.Text += TextBox5.Text;

            # region checkForm

            //Label1.Text = string.Empty;

            // 入力フォームのチェック
            StringBuilder errorMessage = new StringBuilder();
            if (TextBox1.Text == string.Empty)
            {
                errorMessage.Append("ユーザーIDを入力してください。" + Environment.NewLine );
            }
            if (TextBox2.Text == string.Empty)
            {
                errorMessage.Append("パスワードを入力してください。" + Environment.NewLine );
            }
            if (TextBox3.Text == string.Empty)
            {
                errorMessage.Append("デック名称を入力してください。" + Environment.NewLine);
            }
            if ( ( TextBox5.Text == string.Empty
                || TextBox6.Text == string.Empty
                || TextBox7.Text == string.Empty
                || TextBox8.Text == string.Empty
                || TextBox9.Text == string.Empty
                || TextBox10.Text == string.Empty
                || TextBox11.Text == string.Empty
                || TextBox12.Text == string.Empty
                || TextBox13.Text == string.Empty
                || TextBox14.Text == string.Empty
                || TextBox15.Text == string.Empty
                || TextBox16.Text == string.Empty
                || TextBox17.Text == string.Empty
                || TextBox18.Text == string.Empty
                || TextBox19.Text == string.Empty
                || TextBox20.Text == string.Empty
                || TextBox21.Text == string.Empty
                || TextBox22.Text == string.Empty
                || TextBox23.Text == string.Empty
                || TextBox24.Text == string.Empty
                || TextBox25.Text == string.Empty
                || TextBox26.Text == string.Empty
                || TextBox27.Text == string.Empty
                || TextBox28.Text == string.Empty
                || TextBox29.Text == string.Empty
                || TextBox30.Text == string.Empty
                || TextBox31.Text == string.Empty
                || TextBox32.Text == string.Empty
                || TextBox33.Text == string.Empty
                || TextBox34.Text == string.Empty) )
            {
                errorMessage.Append("カードを30枚指定してください。" + Environment.NewLine);
            }
            
            // 更新処理
            if (!password.Equals(string.Empty))
            {
                if (!TextBox2.Text.Equals( password ))
                {
                    errorMessage.Append("パスワードが違います。" + Environment.NewLine);
            
                }
            }

            if (errorMessage.Length > 0)
            {
                Label1.Text = errorMessage.ToString();
                return;
            }

            #endregion


            // 登録処理

            // SqlConnection の新しいインスタンスを生成する (接続文字列を指定)
            System.Data.SqlClient.SqlConnection cSqlConnection = (
                new System.Data.SqlClient.SqlConnection(stConnectionString)
            );

            try
            {
                // データベース接続を開く
                cSqlConnection.Open();

                //cSqlConnection.

                // cSqlConnection から SqlCommand のインスタンスを生成する
                System.Data.SqlClient.SqlCommand hCommand = cSqlConnection.CreateCommand();

                if (deckNo != null && id != null)
                {
                    hCommand.CommandText = "update deckdb "
                    + " set "
                    + " DeckName = '" + TextBox3.Text + "',"
                    +" Remarks = '" + TextBox4.Text + "',"
                    + " URL1 = '" + GetDBValue(TextBox5.Text) + "',"
                    + " URL2 = '" + GetDBValue(TextBox6.Text) + "',"
                    + " URL3 = '" + GetDBValue(TextBox7.Text) + "',"
                    + " URL4 = '" + GetDBValue(TextBox8.Text) + "',"
                    + " URL5 = '" + GetDBValue(TextBox9.Text) + "',"
                    + " URL6 = '" + GetDBValue(TextBox10.Text) + "',"
                    + " URL7 = '" + GetDBValue(TextBox11.Text) + "',"
                    + " URL8 = '" + GetDBValue(TextBox12.Text) + "',"
                    + " URL9 = '" + GetDBValue(TextBox13.Text) + "',"
                    + " URL10 = '" + GetDBValue(TextBox14.Text) + "',"
                    + " URL11 = '" + GetDBValue(TextBox15.Text) + "',"
                    + " URL12 = '" + GetDBValue(TextBox16.Text) + "',"
                    + " URL13 = '" + GetDBValue(TextBox17.Text) + "',"
                    + " URL14 = '" + GetDBValue(TextBox18.Text) + "',"
                    + " URL15 = '" + GetDBValue(TextBox19.Text) + "',"
                    + " URL16 = '" + GetDBValue(TextBox20.Text) + "',"
                    + " URL17 = '" + GetDBValue(TextBox21.Text) + "',"
                    + " URL18 = '" + GetDBValue(TextBox22.Text) + "',"
                    + " URL19 = '" + GetDBValue(TextBox23.Text) + "',"
                    + " URL20 = '" + GetDBValue(TextBox24.Text) + "',"
                    + " URL21 = '" + GetDBValue(TextBox25.Text) + "',"
                    + " URL22 = '" + GetDBValue(TextBox26.Text) + "',"
                    + " URL23 = '" + GetDBValue(TextBox27.Text) + "',"
                    + " URL24 = '" + GetDBValue(TextBox28.Text) + "',"
                    + " URL25 = '" + GetDBValue(TextBox29.Text) + "',"
                    + " URL26 = '" + GetDBValue(TextBox30.Text) + "',"
                    + " URL27 = '" + GetDBValue(TextBox31.Text) + "',"
                    + " URL28 = '" + GetDBValue(TextBox32.Text) + "',"
                    + " URL29 = '" + GetDBValue(TextBox33.Text) + "',"
                    + " URL30 = '" + GetDBValue(TextBox34.Text) + "'"
                    + " where ID = '" + id + "' and  deckno = " + deckNo;

                    hCommand.ExecuteNonQuery();
                    hCommand.Dispose();
                    Label1.Text = Button1.Text + "が完了しました。";
                }
                else
                {

                    // デック通し番号を取得
                    int deckNoValue = 0;

                    hCommand.CommandText = "select count(*) from deckdb where ID = '" + TextBox1.Text + "'";

                    int count = (int)hCommand.ExecuteScalar();

                    if (password.Equals(string.Empty))
                    {
                        deckNoValue = count + 1;
                    }
                    else
                    {
                        deckNoValue = Int32.Parse(deckNo);
                    }

                    // 実行する SQL コマンドを設定する
                    hCommand.CommandText = "INSERT INTO deckdb" +
                        "( ID, DeckNo, Password, Remarks, DeckName, " +
                        "URL1, URL2, URL3, URL4, URL5, URL6, " +
                        "URL7, URL8, URL9, URL10, URL11, URL12, " +
                        "URL13, URL14, URL15, URL16, URL17, URL18, " +
                        "URL19, URL20, URL21, URL22, URL23, URL24, " +
                        "URL25, URL26, URL27, URL28, URL29, URL30) " +
                        " VALUES " +
                        " ( '" + TextBox1.Text + "', '" +
                        deckNoValue + "', '" +
                        TextBox2.Text + "', '" +
                        TextBox4.Text + "', '" +
                        TextBox3.Text + "', '" +
                        GetDBValue(TextBox5.Text) + "', '" +
                        GetDBValue(TextBox6.Text) + "', '" +
                        GetDBValue(TextBox7.Text) + "', '" +
                        GetDBValue(TextBox8.Text) + "', '" +
                        GetDBValue(TextBox9.Text) + "', '" +
                        GetDBValue(TextBox10.Text) + "', '" +
                        GetDBValue(TextBox11.Text) + "', '" +
                        GetDBValue(TextBox12.Text) + "', '" +
                        GetDBValue(TextBox13.Text) + "', '" +
                        GetDBValue(TextBox14.Text) + "', '" +
                        GetDBValue(TextBox15.Text) + "', '" +
                        GetDBValue(TextBox16.Text) + "', '" +
                        GetDBValue(TextBox17.Text) + "', '" +
                        GetDBValue(TextBox18.Text) + "', '" +
                        GetDBValue(TextBox19.Text) + "', '" +
                        GetDBValue(TextBox20.Text) + "', '" +
                        GetDBValue(TextBox21.Text) + "', '" +
                        GetDBValue(TextBox22.Text) + "', '" +
                        GetDBValue(TextBox23.Text) + "', '" +
                        GetDBValue(TextBox24.Text) + "', '" +
                        GetDBValue(TextBox25.Text) + "', '" +
                        GetDBValue(TextBox26.Text) + "', '" +
                        GetDBValue(TextBox27.Text) + "', '" +
                        GetDBValue(TextBox28.Text) + "', '" +
                        GetDBValue(TextBox29.Text) + "', '" +
                        GetDBValue(TextBox30.Text) + "', '" +
                        GetDBValue(TextBox31.Text) + "', '" +
                        GetDBValue(TextBox32.Text) + "', '" +
                        GetDBValue(TextBox33.Text) + "', '" +
                        GetDBValue(TextBox34.Text) + "'" +
                        " ) ";


                    // 指定した SQL コマンドを実行する
                    hCommand.ExecuteNonQuery();

                    hCommand.Dispose();

                    Label1.Text = Button1.Text + "が完了しました。";

                }
            }
            catch (Exception ex)
            {
                Label1.Text = ex.ToString();
            }
            finally
            {
                // データベース接続を閉じる
                cSqlConnection.Close();
                cSqlConnection.Dispose();

            }

            //Response.Redirect("Editor.aspx");

        }

        private string GetDBValue(string url)
        {
            return url.Replace(@"http://digitalmado.com/card/img/", string.Empty).Replace("/card.jpg", string.Empty);

        }

        private string GetUrlValue(string url)
        {
            return @"http://digitalmado.com/card/img/" + url + "/card.jpg";

        }

        protected void TextBox5_TextChanged(object sender, EventArgs e)
        {
            Image1.ImageUrl = ((TextBox)sender).Text;
        }

        protected void Button2_Click(object sender, EventArgs e)
        {
            Image1.ImageUrl = TextBox5.Text;
            Image2.ImageUrl = TextBox6.Text;
            Image3.ImageUrl = TextBox7.Text;
            Image4.ImageUrl = TextBox8.Text;
            Image5.ImageUrl = TextBox9.Text;
            Image6.ImageUrl = TextBox10.Text;
            Image7.ImageUrl = TextBox11.Text;
            Image8.ImageUrl = TextBox12.Text;
            Image9.ImageUrl = TextBox13.Text;
            Image10.ImageUrl = TextBox14.Text;
            Image11.ImageUrl = TextBox15.Text;
            Image12.ImageUrl = TextBox16.Text;
            Image13.ImageUrl = TextBox17.Text;
            Image14.ImageUrl = TextBox18.Text;
            Image15.ImageUrl = TextBox19.Text;
            Image16.ImageUrl = TextBox20.Text;
            Image17.ImageUrl = TextBox21.Text;
            Image18.ImageUrl = TextBox22.Text;
            Image19.ImageUrl = TextBox23.Text;
            Image20.ImageUrl = TextBox24.Text;
            Image21.ImageUrl = TextBox25.Text;
            Image22.ImageUrl = TextBox26.Text;
            Image23.ImageUrl = TextBox27.Text;
            Image24.ImageUrl = TextBox28.Text;
            Image25.ImageUrl = TextBox29.Text;
            Image26.ImageUrl = TextBox30.Text;
            Image27.ImageUrl = TextBox31.Text;
            Image28.ImageUrl = TextBox32.Text;
            Image29.ImageUrl = TextBox33.Text;
            Image30.ImageUrl = TextBox34.Text;
        }
    }
}
